/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.core;

import etrl.vecmath.Vector3f;
import etrl.vecmath.Vector4f;

/**
 * \class BoundingBox
 * \brief Represents a bounding box with basic operations.
 * @author harold
 *
 */
public class BoundingBox
{
	/**
	 * Coordinates of the lower left corner
	 */
	private Vector3f _min;
	
	/**
	 * Coordinates of the upper right corner
	 */
	private Vector3f _max;
	
	/**
	 * \brief Initializes a bounding box with the given bounds.
	 * @param[in] min Coordinates of the lower left corner to set.
	 * @param[in] max Coordinates of the upper right corner to set.
	 */
	public BoundingBox(Vector3f min, Vector3f max)
	{
		_min = new Vector3f(min);
		_max = new Vector3f(max);
	}
	
	/**
	 * \brief Initializes a bounding box without bounds.
	 */
	public BoundingBox()
	{
		_min = new Vector3f(0.f, 0.f, 0.f);
		_max = new Vector3f(0.f, 0.f, 0.f);
	}
	
	/**
	 * \brief Clones the bounding box.
	 * @return The bounding box cloned.
	 */
	public BoundingBox clone()
	{
		return new BoundingBox(_min, _max);
	}
	
	/**
	 * \brief Coordinates of the lower left corner getter 
	 * @return Lower left corner coords.
	 */
	public Vector3f getMin()
	{
		return new Vector3f(_min);
	}
	
	/**
	 * \brief Coordinates of the upper right corner getter.
	 * @return Upper right corner coords.
	 */
	public Vector3f getMax()
	{
		return new Vector3f(_max);
	}
	
	/**
	 * \brief Coordinates of the lower left corner setter
	 * @param[in] min Lower left corner coords to set
	 */
	public void setMin(Vector3f min)
	{
		_min = new Vector3f(min);
	}
	
	/**
	 * \brief Coordinates of the upper right corner setter
	 * @param[in] max Upper right coords to set
	 */
	public void setMax(Vector3f max)
	{
		_max = new Vector3f(max);
	}
	
	/**
	 * \brief Computes the bounding sphere containing the bounding box.
	 * @return Computed bounding sphere.
	 */
	public BoundingSphere asBoundingSphere()
	{
		// Create an empty bounding sphere
		BoundingSphere bsphere = new BoundingSphere();
		
		// The bounding sphere center is the same as the bounding box one
		Vector3f center = new Vector3f(_min);
		center.add(_max);
		center.scale(0.5f);
		bsphere.setCenter(new Vector4f(center.x, center.y, center.z, 1.f));
		
		// And the radius is half of the length of min-max vector
		Vector3f radiusVect = new Vector3f(_max);
		radiusVect.sub(_min);
		bsphere.setRadius(radiusVect.length() * 0.5f);
			
		return bsphere;
	}
	
	/**
	 * \brief Checks if the given ray intersects the bounding sphere containing the bounding box.
	 * @param[in] start Coordinates of the ray start point.
	 * @param[in] end Coordinates of the ray end point.
	 * @return true if the ray intersects or is included in the bounding sphere, else false
	 */
	public boolean intersectsRay(Vector4f start, Vector4f end)
	{	
		return asBoundingSphere().intersectsRay(start, end);
	}
}
